package web;
import strategies.*;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
/**
 *
 * @author Alejandro
 */
public class Linker {
    public static final int MSG_CREATE_DAO = 1;
    public static final int MSG_DELETE_ITEMS = 2;
    public static final int MSG_ADD_ROW = 3;
    public static final int MSG_EDIT_ROW = 4;
    public static final int MSG_EDIT_PAGE = 5;
    public static final int MSG_GROUP_EDIT_PAGE = 6;
    public static final int MSG_GROUP_EDIT_ROW = 7;
    public static final int MSG_VIEW_TABLE = 10;
    // TODO : add other message id's
    private static Linker instance=null;
    private static Map<Integer, IStrategy> messageMap;
    
    
    private static Map<String, Integer> defaultSubmitMap = new TreeMap<String, Integer>();
    private static Map<Integer,  Map<Integer, IStrategy>> deepFindStrategyMap=new TreeMap <Integer,  Map<Integer, IStrategy>>();
    private static Map<Integer, IStrategy> addMap=new TreeMap<Integer, IStrategy>();
    private static Map<Integer, IStrategy> editMap=new TreeMap<Integer, IStrategy>();
    private static Map<Integer, IStrategy> deleteMap=new TreeMap<Integer, IStrategy>();
    private static Map<Integer, IStrategy> editPageMap=new TreeMap<Integer, IStrategy>();
    private static Map<Integer, IStrategy> createDAOMap=new TreeMap<Integer, IStrategy>();
    private static Map<Integer, IStrategy> groupEditPageMap=new TreeMap<Integer, IStrategy>();
    private static Map<Integer, IStrategy> groupEditRowMap=new TreeMap<Integer, IStrategy>();
    //TO DO : Initialized maps
    {
        defaultSubmitMap.put("delete", MSG_DELETE_ITEMS);
        defaultSubmitMap.put("table-selection", MSG_CREATE_DAO);
        defaultSubmitMap.put("new_add", MSG_ADD_ROW);
        defaultSubmitMap.put("new_edit", MSG_EDIT_ROW);
        defaultSubmitMap.put("new", MSG_EDIT_PAGE);
        defaultSubmitMap.put("edit", MSG_EDIT_PAGE);
        defaultSubmitMap.put("moveto", MSG_GROUP_EDIT_PAGE);
        defaultSubmitMap.put("group_edit_row", MSG_GROUP_EDIT_ROW);

        addMap.put(1, new EmployeesAddRowStrategy());
        addMap.put(2, new DepartmentsAddRowStrategy());
        addMap.put(3, new CountriesAddRowStrategy());

        editMap.put(1, new EmployeesEditRowStrategy());
        editMap.put(2, new DepartmentsEditRowStrategy());
        editMap.put(3, new CountriesEditRowStrategy());

        deleteMap.put(0, new DeleteItemsStrategy());
        deleteMap.put(1, new DeleteItemsStrategy());
        deleteMap.put(2, new DeleteItemsStrategy());
        deleteMap.put(3, new DeleteItemsStrategy());

        editPageMap.put(0, new NotSupportedOperationStrategy());
        editPageMap.put(1, new EmployeesEditPageStrategy());
        editPageMap.put(2, new DepartmentsEditPageStrategy());
        editPageMap.put(3, new CountriesEditPageStrategy());

        createDAOMap.put(0, new CreateDAOStrategy());
        createDAOMap.put(1, new CreateDAOStrategy());
        createDAOMap.put(2, new CreateDAOStrategy());
        createDAOMap.put(3, new CreateDAOStrategy());

        groupEditPageMap.put(0,new NotSupportedOperationStrategy());
        groupEditPageMap.put(1,new GroupEmployeesEditStrategy());
        groupEditPageMap.put(2,new NotSupportedOperationStrategy());
        groupEditPageMap.put(3,new NotSupportedOperationStrategy());

        groupEditRowMap.put(1,new GroupEditRowsStrategy());
        //groupEditRowMap.put(2,new GroupEditRowsStrategy());
        //groupEditRowMap.put(3,new GroupEditRowsStrategy());

        deepFindStrategyMap.put(MSG_DELETE_ITEMS, deleteMap);
        deepFindStrategyMap.put(MSG_CREATE_DAO, createDAOMap);
        deepFindStrategyMap.put(MSG_ADD_ROW, addMap);
        deepFindStrategyMap.put(MSG_EDIT_ROW, editMap);
        deepFindStrategyMap.put(MSG_EDIT_PAGE, editPageMap);
        deepFindStrategyMap.put(MSG_GROUP_EDIT_PAGE, groupEditPageMap);
        deepFindStrategyMap.put(MSG_GROUP_EDIT_ROW, groupEditRowMap);
    }

    private Linker() {
       messageMap=new TreeMap<Integer, IStrategy>();
    }
    public static Linker getInstance() {
        try {
            if (instance == null) {
                instance = new Linker();
            }
            return instance;
        } catch (Exception e) {
            return null;
        }
    }
    public static void setMessageMap(HttpServletRequest request){
        messageMap.clear();
        Enumeration names = request.getParameterNames();
        while(names.hasMoreElements()) {
            String key = (String) names.nextElement();
            if (defaultSubmitMap.containsKey(key)) {
                Integer i = defaultSubmitMap.get(key);
                if (deepFindStrategyMap.containsKey(i)) {
                    IStrategy strategy = deepFindStrategyMap.get(i).get(Integer.parseInt(request.getParameter("table-selection")));
                    messageMap.put(i, strategy);
                }
            }
        }
        messageMap.put(MSG_VIEW_TABLE, new ViewTableStrategy());
    }
    public static Map<Integer, IStrategy> getMessageMap() {
        return messageMap;
    }

}
